﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using VSD.OneStopService.BLL;


public partial class MasterPage : System.Web.UI.MasterPage
{
    StaffRepository resStaff = new StaffRepository();

    private List<string> loginParameter = new List<string>();

    public string LastText = string.Empty;

    private List<Staff> ChuyenVienPhong(int departmentID)
    {
        string roleChuyenVien = PermissionManager.ConvertCodeToRole(PermissionCode.CHUYENVIEN_XULY);
        string[] AllChuyenVien = Roles.GetUsersInRole(roleChuyenVien);// Danh sách có quyền chuyen vien
        List<Staff> TempChuyenViens = resStaff.GetStaffByUserNames(AllChuyenVien);
        List<Staff> DSChuyenVien = new List<Staff>();
        foreach (Staff s in TempChuyenViens)
        {
            if (s.DepartmentID == departmentID)
            {
                DSChuyenVien.Add(s);
            }
        }
        return DSChuyenVien;
    }

    private List<Staff> LanhDaoPhong(int departmentID)
    {
        string roleTruongPhong = PermissionManager.ConvertCodeToRole(PermissionCode.LANHDAO_PHONG_CHUYENMON);
        string[] TruongPhongs = Roles.GetUsersInRole(roleTruongPhong);// Danh sách có quyền trưởng phòng

        return resStaff.GetStaffByUserNames(TruongPhongs);
    }

    private List<Staff> LanhDaoCoQuan()
    {
        try
        {
            string roleLanhDao = PermissionManager.ConvertCodeToRole(PermissionCode.LANHDAO_COQUAN);
            string roleLanhDaoVPs = PermissionManager.ConvertCodeToRole(PermissionCode.LANHDAO_VP);

            string[] LanhDaos = Roles.GetUsersInRole(roleLanhDao);// Danh sách có quyền lãnh đạo cơ quan
            string[] LanhDaoVPs = Roles.GetUsersInRole(roleLanhDaoVPs);// Danh sách có quyền ký hồ sơ

            List<Staff> lanhDaoVP = resStaff.GetStaffByUserNames(LanhDaoVPs);
            ContextUser.DSLanhDaoVP = lanhDaoVP;

            List<Staff> lanhDaoVPKy = new List<Staff>();
            foreach (Staff item in lanhDaoVP)
            {
                PermissionManager p = new PermissionManager(item.UserName);
                if (p.HasPermission(PermissionCode.KY_HOSO))
                {
                    lanhDaoVPKy.Add(item);
                }
            }
            ContextUser.DSLanhDaoVPKy = lanhDaoVPKy;

            List<Staff> staffs = new List<Staff>();
            staffs.AddRange(resStaff.GetStaffByUserNames(LanhDaos));
            foreach (Staff s in lanhDaoVPKy)
            {
                if (!staffs.Contains(s))
                    staffs.Add(s);
            }
            return staffs;
        }
        catch { return null; }
    }


    private void GetUserContext()
    {
        if (HttpContext.Current.User.Identity.IsAuthenticated && ContextUser.CurrentUser == null)
        {
            string logedUserName = HttpContext.Current.User.Identity.Name;

            StaffRepository staffRepository = new StaffRepository();

            ContextUser.IsLogged = true;
            ContextUser.UserName = logedUserName;

            //Context.CurrentUser = new StaffRepository().GetStaffByUserName(txtUserName.Text.Trim());

            Staff staff = staffRepository.GetStaffByUserName(logedUserName);
            if (staff == null)
            {
                staff = new Staff();
                staff.UserName = logedUserName;
                staffRepository.Add(staff);
                staffRepository.SubmitChanges();
            }

            ContextUser.CurrentUser = staff;
            ContextUser.PermissionCollection = new PermissionManager(staff.UserName);
            try
            {
                ContextUser.CurrentDepartment = new DepartmentRepository().Get(staff.DepartmentID.Value);
                ContextUser.DSChuyenVien = ChuyenVienPhong(staff.DepartmentID.Value);
                ContextUser.DSLanhDaoPhong = LanhDaoPhong(staff.DepartmentID.Value);
            }
            catch { }
            ContextUser.DSLanhDaoCQ = LanhDaoCoQuan();

            CustomizationRepository resCustomize = new CustomizationRepository();
            ContextUser.GlobalOption = resCustomize.GetGlobalOption();
            ContextUser.IndividualOption = resCustomize.GetByUserName(logedUserName);
            string IDs = resCustomize.GetServiceIDsByUserName(logedUserName);
            if (!string.IsNullOrEmpty(IDs))
            {
                string temp = IDs.Substring(1);
                ContextUser.ServiceQuickSelect = temp.Split(',').ToList();
            }
            else
            {
                ContextUser.ServiceQuickSelect = new List<string>();
            }

            if (logedUserName == "administrator")
            {
                ContextUser.IsSystemAccount = true;
            }
        }
    }


    protected void Page_Load(object sender, EventArgs e)
    {
        setPermissionExcute();
        ContextUser.UserName = Convert.ToString(HttpContext.Current.Session["AdminLogined"]);
        if (!IsPostBack)
        {
            if (HttpContext.Current.Session["AdminLogined"] != null)
            {
                if (ContextUser.IsLogged)
                {
                    TenDangNhapMenuItem.Text = ContextUser.CurrentUser.Name;
                }
                else
                {
                    Response.Redirect("~/Login.aspx");
                }
            }
            else
            {
                Response.Redirect("~/Login.aspx");
            }

        }


        //Url
        string url = Request.RawUrl.ToLower();
        PageLink link = PageLinks.Pages().Find(x => url.Contains(x.Url));
        if (link != null)
            LastText = link.Name;
        //LastText = url;

    }

    public void setPermissionExcute()
    {
        // menu Xử lý hồ sơ 

        this.HSChoKyMenuItem.Visible = false;
        this.HSDaKyMenuItem.Visible = false;
        this.HSChoXuLy.Visible = false;
        this.HSDangXuLy.Visible = false;
        this.HSChoPhanXL.Visible = false;
        try
        {

            // menu Xử lý hồ sơ 
            this.HSChoPhanXL.Visible = (ContextUser.PermissionCollection.HasPermission(PermissionCode.LANHDAO_COQUAN)) || (ContextUser.PermissionCollection.HasPermission(PermissionCode.LANHDAO_PHONG_CHUYENMON));
            this.HSDangXuLy.Visible = (ContextUser.PermissionCollection.HasPermission(PermissionCode.LANHDAO_COQUAN)) || (ContextUser.PermissionCollection.HasPermission(PermissionCode.LANHDAO_PHONG_CHUYENMON)) || (ContextUser.PermissionCollection.HasPermission(PermissionCode.CHUYENVIEN_XULY));
            this.HSChoXuLy.Visible = (ContextUser.PermissionCollection.HasPermission(PermissionCode.LANHDAO_COQUAN)) || (ContextUser.PermissionCollection.HasPermission(PermissionCode.LANHDAO_PHONG_CHUYENMON)) || (ContextUser.PermissionCollection.HasPermission(PermissionCode.CHUYENVIEN_XULY));
            this.HSChoKyMenuItem.Visible = (ContextUser.PermissionCollection.HasPermission(PermissionCode.LANHDAO_COQUAN)) || (ContextUser.PermissionCollection.HasPermission(PermissionCode.LANHDAO_PHONG_CHUYENMON)) || (ContextUser.PermissionCollection.HasPermission(PermissionCode.CHUYENVIEN_XULY));
            this.HSDaKyMenuItem.Visible = (ContextUser.PermissionCollection.HasPermission(PermissionCode.LANHDAO_COQUAN)) || (ContextUser.PermissionCollection.HasPermission(PermissionCode.LANHDAO_PHONG_CHUYENMON)) || (ContextUser.PermissionCollection.HasPermission(PermissionCode.CHUYENVIEN_XULY));

        }
        catch { }
    }



}
